{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SsIv6LYT84gm"
      },
      "source": [
        "# Conversion of COCO annotation JSON file to TFRecords"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zl7o2xEW9IbX"
      },
      "source": [
        "Given a COCO annotated JSON file, your goal is to convert it into a TFRecords  file necessary to train with the Mask RCNN model.\n",
        "\n",
        "To accomplish this task, you will clone the TensorFlow Model Garden repo. The TensorFlow Model Garden is a repository with a number of different implementations of state-of-the-art (SOTA) models and modeling solutions for TensorFlow users.\n",
        "\n",
        "This notebook is an end to end example. When you run the notebook, it will take COCO annotated JSON train and test files as an input and will convert them into TFRecord files. You can also output sharded TFRecord files in case your training and validation data is huge. It makes it easier for the algorithm to read and access the data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "g3OHfWQBpYVB"
      },
      "source": [
        "**Note** - In this example, we assume that all our data is saved on Google drive and we will also write our outputs to Google drive. We also assume that the script will be used as a Google Colab notebook. But this can be changed according to the needs of users. They can modify this in case they are working on their local workstation, remote server or any other database. This colab notebook can be changed to a regular jupyter notebook running on a local machine according to the need of the users."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CRwVTTPuED_1"
      },
      "source": [
        "## Run the below command to connect to your google drive"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "pnsra7Zf0uGe"
      },
      "outputs": [],
      "source": [
        "!pip install -q tf-nightly\n",
        "!pip install -q tensorflow-addons"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "bBN0CZWlD7zl"
      },
      "outputs": [],
      "source": [
        "# import libraries\n",
        "from google.colab import drive\n",
        "import sys"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "z5HNdeBp0x3G"
      },
      "outputs": [],
      "source": [
        "# \"opencv-python-headless\" version should be same of \"opencv-python\"\n",
        "import pkg_resources\n",
        "version_number = pkg_resources.get_distribution(\"opencv-python\").version\n",
        "\n",
        "!pip install -q opencv-python-headless==$version_number"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i80tEP0pEJif",
        "outputId": "cb0d8dde-8852-49eb-e6d7-33653722eee0"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Mounted at /content/gdrive\n",
            "Successful\n"
          ]
        }
      ],
      "source": [
        "# connect to google drive\n",
        "drive.mount('/content/gdrive')\n",
        "\n",
        "# making an alias for the root path\n",
        "try:\n",
        "  !ln -s /content/gdrive/My\\ Drive/ /mydrive\n",
        "  print('Successful')\n",
        "except Exception as e:\n",
        "  print(e)\n",
        "  print('Not successful')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "w40-VpWXU-Hu"
      },
      "source": [
        "## Clone TensorFlow Model Garden repository"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Vh42KtozpqeT"
      },
      "outputs": [],
      "source": [
        "# clone the Model Garden directory for Tensorflow where all the config files and scripts are located for this project.\n",
        "# project folder name is - 'waste_identification_ml'\n",
        "!git clone https://github.com/tensorflow/models.git"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wm-k6-S4pr_B"
      },
      "outputs": [],
      "source": [
        "# Go to the model folder\n",
        "%cd models"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xNe2NuqjV4uW"
      },
      "source": [
        "## Create TFRecord for training data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "J9Nz75g0oJkI"
      },
      "outputs": [],
      "source": [
        "training_images_folder = '/mydrive/gtech/total_images/'  #@param {type:\"string\"}\n",
        "training_annotation_file = '/mydrive/gtech/_train.json'  #@param {type:\"string\"}\n",
        "output_folder = '/mydrive/gtech/train/'  #@param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "mjsai7PDAxgp",
        "outputId": "c78c7eaa-36e0-48e0-ba2c-3e674bdc5402"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "I0422 00:06:23.072771 139705362556800 create_coco_tf_record.py:494] writing to output path: /mydrive/gtech/MRFs/Recykal/Latest_sharing_by_sanket/Google_Recykal/Taxonomy_version_2/train/\n",
            "I0422 00:06:25.089654 139705362556800 create_coco_tf_record.py:366] Building bounding box index.\n",
            "I0422 00:06:25.115955 139705362556800 create_coco_tf_record.py:377] 0 images are missing bboxes.\n",
            "I0422 00:07:39.273266 139705362556800 tfrecord_lib.py:168] On image 0\n",
            "I0422 00:09:03.214606 139705362556800 tfrecord_lib.py:168] On image 100\n",
            "I0422 00:10:14.332473 139705362556800 tfrecord_lib.py:168] On image 200\n",
            "I0422 00:11:11.556596 139705362556800 tfrecord_lib.py:168] On image 300\n",
            "I0422 00:12:11.437826 139705362556800 tfrecord_lib.py:168] On image 400\n",
            "I0422 00:13:13.166231 139705362556800 tfrecord_lib.py:168] On image 500\n",
            "I0422 00:14:21.695016 139705362556800 tfrecord_lib.py:168] On image 600\n",
            "I0422 00:15:24.191824 139705362556800 tfrecord_lib.py:168] On image 700\n",
            "I0422 00:16:48.620902 139705362556800 tfrecord_lib.py:168] On image 800\n",
            "I0422 00:17:48.565592 139705362556800 tfrecord_lib.py:168] On image 900\n",
            "I0422 00:18:41.091029 139705362556800 tfrecord_lib.py:168] On image 1000\n",
            "I0422 00:19:39.844225 139705362556800 tfrecord_lib.py:168] On image 1100\n",
            "I0422 00:20:45.108587 139705362556800 tfrecord_lib.py:168] On image 1200\n",
            "I0422 00:22:13.738559 139705362556800 tfrecord_lib.py:168] On image 1300\n",
            "I0422 00:23:13.147292 139705362556800 tfrecord_lib.py:168] On image 1400\n",
            "I0422 00:24:06.315325 139705362556800 tfrecord_lib.py:168] On image 1500\n",
            "I0422 00:24:59.421572 139705362556800 tfrecord_lib.py:168] On image 1600\n",
            "I0422 00:25:45.958540 139705362556800 tfrecord_lib.py:168] On image 1700\n",
            "I0422 00:26:35.475085 139705362556800 tfrecord_lib.py:168] On image 1800\n",
            "I0422 00:27:38.255803 139705362556800 tfrecord_lib.py:168] On image 1900\n",
            "I0422 00:28:37.250636 139705362556800 tfrecord_lib.py:168] On image 2000\n",
            "I0422 00:29:38.937792 139705362556800 tfrecord_lib.py:168] On image 2100\n",
            "I0422 00:30:24.683607 139705362556800 tfrecord_lib.py:168] On image 2200\n",
            "I0422 00:31:13.964802 139705362556800 tfrecord_lib.py:168] On image 2300\n",
            "I0422 00:32:06.411041 139705362556800 tfrecord_lib.py:168] On image 2400\n",
            "I0422 00:33:06.038232 139705362556800 tfrecord_lib.py:168] On image 2500\n",
            "I0422 00:34:15.721037 139705362556800 tfrecord_lib.py:168] On image 2600\n",
            "I0422 00:35:19.886712 139705362556800 tfrecord_lib.py:168] On image 2700\n",
            "I0422 00:36:32.834578 139705362556800 tfrecord_lib.py:168] On image 2800\n",
            "I0422 00:38:00.137243 139705362556800 tfrecord_lib.py:168] On image 2900\n",
            "I0422 00:39:24.083769 139705362556800 tfrecord_lib.py:168] On image 3000\n",
            "I0422 00:40:47.815561 139705362556800 tfrecord_lib.py:168] On image 3100\n",
            "I0422 00:42:01.868806 139705362556800 tfrecord_lib.py:168] On image 3200\n",
            "I0422 00:43:10.464518 139705362556800 tfrecord_lib.py:168] On image 3300\n",
            "I0422 00:44:08.492330 139705362556800 tfrecord_lib.py:168] On image 3400\n",
            "I0422 00:45:06.637591 139705362556800 tfrecord_lib.py:168] On image 3500\n",
            "I0422 00:46:17.144057 139705362556800 tfrecord_lib.py:168] On image 3600\n",
            "I0422 00:47:34.219212 139705362556800 tfrecord_lib.py:168] On image 3700\n",
            "I0422 00:48:47.535176 139705362556800 tfrecord_lib.py:168] On image 3800\n",
            "I0422 00:49:44.018001 139705362556800 tfrecord_lib.py:168] On image 3900\n",
            "I0422 00:50:46.843277 139705362556800 tfrecord_lib.py:168] On image 4000\n",
            "I0422 00:51:42.749161 139705362556800 tfrecord_lib.py:168] On image 4100\n",
            "I0422 00:52:29.118489 139705362556800 tfrecord_lib.py:168] On image 4200\n",
            "I0422 00:53:12.499863 139705362556800 tfrecord_lib.py:168] On image 4300\n",
            "I0422 00:54:02.751904 139705362556800 tfrecord_lib.py:168] On image 4400\n",
            "I0422 00:54:54.855237 139705362556800 tfrecord_lib.py:168] On image 4500\n",
            "I0422 00:56:11.432259 139705362556800 tfrecord_lib.py:168] On image 4600\n",
            "I0422 00:57:12.901312 139705362556800 tfrecord_lib.py:168] On image 4700\n",
            "I0422 00:58:15.347571 139705362556800 tfrecord_lib.py:168] On image 4800\n",
            "I0422 00:59:13.046698 139705362556800 tfrecord_lib.py:168] On image 4900\n",
            "I0422 01:00:38.408758 139705362556800 tfrecord_lib.py:168] On image 5000\n",
            "I0422 01:02:03.484946 139705362556800 tfrecord_lib.py:168] On image 5100\n",
            "I0422 01:02:57.290261 139705362556800 tfrecord_lib.py:168] On image 5200\n",
            "I0422 01:03:54.188467 139705362556800 tfrecord_lib.py:168] On image 5300\n",
            "I0422 01:04:49.160263 139705362556800 tfrecord_lib.py:168] On image 5400\n",
            "I0422 01:05:46.782065 139705362556800 tfrecord_lib.py:168] On image 5500\n",
            "I0422 01:07:00.913060 139705362556800 tfrecord_lib.py:168] On image 5600\n",
            "I0422 01:08:05.558512 139705362556800 tfrecord_lib.py:168] On image 5700\n",
            "I0422 01:09:09.658477 139705362556800 tfrecord_lib.py:168] On image 5800\n",
            "I0422 01:10:10.147291 139705362556800 tfrecord_lib.py:168] On image 5900\n",
            "I0422 01:11:11.286698 139705362556800 tfrecord_lib.py:168] On image 6000\n",
            "I0422 01:12:08.696386 139705362556800 tfrecord_lib.py:168] On image 6100\n",
            "I0422 01:13:02.225769 139705362556800 tfrecord_lib.py:168] On image 6200\n",
            "I0422 01:13:55.910152 139705362556800 tfrecord_lib.py:168] On image 6300\n",
            "I0422 01:14:47.861520 139705362556800 tfrecord_lib.py:181] Finished writing, skipped 8 annotations.\n",
            "I0422 01:14:47.862285 139705362556800 create_coco_tf_record.py:529] Finished writing, skipped 8 annotations.\n"
          ]
        }
      ],
      "source": [
        "# run the script to convert your json file to TFRecord file\n",
        "# --num_shards (how many TFRecord sharded files you want)\n",
        "!python3 -m official.vision.data.create_coco_tf_record \\\n",
        "      --logtostderr \\\n",
        "      --image_dir=$training_images_folder \\\n",
        "      --object_annotations_file=$training_annotation_file \\\n",
        "      --output_file_prefix=$output_folder \\\n",
        "      --num_shards=100 \\\n",
        "      --include_masks=True \\\n",
        "      --num_processes=0"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "zwazp89SojMA"
      },
      "source": [
        "## Create TFRecord for validation data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OVQn5DiFBUfv"
      },
      "outputs": [],
      "source": [
        "validation_annotation_file = '/mydrive/gtech/total_images/'  #@param {type:\"string\"}\n",
        "validation_data_folder = '/mydrive/gtech/_val.json'  #@param {type:\"string\"}\n",
        "output_folder = '/mydrive/gtech/val/'  #@param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nWbKeLoVwXbi",
        "outputId": "63f4fc03-43b1-424e-dfb2-200f9bbdf1e5"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "I0421 20:53:39.071351 140304098097024 create_coco_tf_record.py:494] writing to output path: /mydrive/gtech/MRFs/Recykal/Latest_sharing_by_sanket/Google_Recykal/Taxonomy_version_2/val/\n",
            "I0421 20:53:40.622877 140304098097024 create_coco_tf_record.py:366] Building bounding box index.\n",
            "I0421 20:53:40.627101 140304098097024 create_coco_tf_record.py:377] 0 images are missing bboxes.\n",
            "I0421 20:54:41.275259 140304098097024 tfrecord_lib.py:168] On image 0\n",
            "I0421 20:56:53.052898 140304098097024 tfrecord_lib.py:168] On image 100\n",
            "I0421 20:59:01.886727 140304098097024 tfrecord_lib.py:168] On image 200\n",
            "I0421 21:01:12.356394 140304098097024 tfrecord_lib.py:168] On image 300\n",
            "I0421 21:03:03.635432 140304098097024 tfrecord_lib.py:168] On image 400\n",
            "I0421 21:05:04.787051 140304098097024 tfrecord_lib.py:168] On image 500\n",
            "I0421 21:06:52.991898 140304098097024 tfrecord_lib.py:168] On image 600\n",
            "I0421 21:09:02.626780 140304098097024 tfrecord_lib.py:168] On image 700\n",
            "I0421 21:11:39.070799 140304098097024 tfrecord_lib.py:168] On image 800\n",
            "I0421 21:13:58.603258 140304098097024 tfrecord_lib.py:168] On image 900\n",
            "I0421 21:16:23.214870 140304098097024 tfrecord_lib.py:168] On image 1000\n",
            "I0421 21:18:25.072518 140304098097024 tfrecord_lib.py:168] On image 1100\n",
            "I0421 21:20:29.223420 140304098097024 tfrecord_lib.py:168] On image 1200\n",
            "I0421 21:22:34.431273 140304098097024 tfrecord_lib.py:168] On image 1300\n",
            "I0421 21:24:29.066092 140304098097024 tfrecord_lib.py:168] On image 1400\n",
            "I0421 21:26:33.851860 140304098097024 tfrecord_lib.py:168] On image 1500\n",
            "I0421 21:28:25.426244 140304098097024 tfrecord_lib.py:168] On image 1600\n",
            "I0421 21:28:59.923923 140304098097024 tfrecord_lib.py:181] Finished writing, skipped 2 annotations.\n",
            "I0421 21:28:59.924295 140304098097024 create_coco_tf_record.py:529] Finished writing, skipped 2 annotations.\n"
          ]
        }
      ],
      "source": [
        "# run the script to convert your json file to TFRecord file\n",
        "# --num_shards (how many TFRecord sharded files you want)\n",
        "!python3 -m official.vision.data.create_coco_tf_record --logtostderr \\\n",
        "      --image_dir=$validation_images_folder \\\n",
        "      --object_annotations_file=$validation_annotation_file \\\n",
        "      --output_file_prefix=$output_folder \\\n",
        "      --num_shards=100 \\\n",
        "      --include_masks=True \\\n",
        "      --num_processes=0"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "machine_shape": "hm",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
